<template>
  <el-dialog
    title="分配角色"
    :visible="setRoleDialog"
    width="50%"
    @close="onClose"
    @opened="onOpened"
  >
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roles" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button :disabled="!checkList.length" :loading="onSaveLoading" type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getroleList } from '@/api/company.js'
import { getInfoById } from '@/api/user'
import { updateEmployeeRoles } from '@/api/employees'
export default {
  name: '',
  props: {
    setRoleDialog: {
      type: Boolean,
      required: true
    },
    employeesId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      roles: [],
      onSaveLoading: false
    }
  },
  created() {
    this.loadRolesList()
  },
  // 定义方法
  methods: {
    async loadRolesList() {
      const res = await getroleList()
      const { rows: resData } = await getroleList({ page: 1, pagesize: res.total })
      this.roles = resData
    },
    async loademployeesRoleId() {
      const res = await getInfoById(this.employeesId)
      this.checkList = res.roleIds
    },
    onOpened() {
      this.loademployeesRoleId()
    },
    onClose() {
      this.$emit('update:set-role-dialog', false)
      this.checkList = []
    },
    async onSave() {
      if (!this.checkList.length) {
        return false
      }
      this.onSaveLoading = true

      await updateEmployeeRoles({
        id: this.employeesId,
        roleIds: this.checkList
      })
      this.onClose()
      this.onSaveLoading = false
    }
  }

}
</script>

<style scoped lang='less'>

</style>
